package com.hectopascal.goalance.repository;

import com.hectopascal.goalance.pojo.Notification;
import com.hectopascal.goalance.pojo.dto.NotificationBack;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Repository
public interface NotificationRepository extends CrudRepository<Notification,Integer> {
    @Query(value = "SELECT * FROM notification WHERE (user_id = :userId)", nativeQuery = true)
    Optional<List<NotificationBack>> getAllNotification(Integer userId);

    @Query(value = "SELECT * FROM notification WHERE (author_id = :userId)", nativeQuery = true)
    Optional<List<NotificationBack>> getSendNotification(Integer userId);

    @Modifying
    @Transactional
    @Query(value = "UPDATE notification SET is_read = 1 WHERE id = :id", nativeQuery = true)
    int updateIsRead(Integer id);

    @Modifying
    @Transactional
    @Query(value = "DELETE from notification WHERE id = :id", nativeQuery = true)
    int deleteNotification(Integer id);

    @Query(value = "SELECT * FROM notification WHERE (user_id = :userId) AND (is_read = 0)", nativeQuery = true)
    Optional<List<NotificationBack>> getUnreadNotification(Integer userId);
}
